package com.ggs.rg.ybjb.usermodule.controller;

import com.alibaba.fastjson.JSONObject;
import com.ggs.rg.ybjb.pojomodule.pojo.TUser;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.usermodule.service.UserService;
import com.ggs.rg.ybjb.usermodule.util.ImageUtil;
import com.ggs.rg.ybjb.usermodule.util.UserUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * ClassName:UserController
 * Package:com.ggs.rg.ybjb.usermodule.controller
 * Description:
 * 用户模块控制层,功能:查询用户信息.更新用户信息
 * @Date:2021/11/22 20:43
 * @Author:cxw
 */
@RestController
@RequestMapping("/userinfo")
public class UserController {

    @Autowired
    private UserService userService;

    //根据用户手机号查询指定用户
    @RequestMapping(value = "/getUserInfoByPhone",method = RequestMethod.GET)
    public JSONObject getUserInfo(@RequestParam(value = "phone",required = false)String phone){
        TUser user = userService.getById(phone);
        if (StringTools.isNullOrEmpty(user))
            return CommonUtil.sqlErrorJson("没有查到手机号为:" + phone + "的用户");
        //调用工具类,去除多余的信息,将用户的信息封装成json对象
        JSONObject resultList = UserUtil.getResultList(user,"9002");
        return CommonUtil.successJson(resultList);
    }

    //根据手机号更新用户信息
    @PostMapping("/updateUserInfo")
    public JSONObject updateUserInfo(@RequestParam String phone,
                                     @RequestParam(required = false) String name,
                                     @RequestParam(required = false) MultipartFile image,
                                     @RequestParam(required = false) Boolean sex,
                                     @RequestParam(required = false) String desc,
                                     @RequestParam(required = false) String email,
                                     @RequestParam(required = false) Double money) throws IOException {
        TUser dataUser = userService.getById(phone);
        if (dataUser == null)
            return CommonUtil.sqlErrorJson("查无此用户");
        TUser user = new TUser(phone);
        String imageName = null;
        if (name != null)
            user.setName(name);
        if (sex != null)
            user.setSex(sex);
        if (desc != null)
            user.setPDesc(desc);
        if (email != null)
            user.setEmail(email);
        if (money != null)
            user.setMoney(money);
        if (image != null && !image.getOriginalFilename().equals(dataUser.getImage())){
            System.out.println("用户上传了新头像!!!");
            imageName = ImageUtil.getNewFileName(image);
        }
        if (imageName != null)
            user.setImage(imageName);
        boolean result = userService.updateById(user);
        if (result && imageName != null){
            //如果数据库更新成功,将更新图片保存到服务器,删除原图片
            ImageUtil.saveFile(imageName,image);
            ImageUtil.deleteFile(dataUser.getImage());
            return CommonUtil.successJson();
        }else if (result){
            return CommonUtil.successJson();
        }
        return CommonUtil.errorJson("500","未知错误");
    }

//    @PostMapping("/test/{id}")
//    public String test(@PathVariable("id")Integer id,@RequestParam String name){
//        System.out.println(id);
//        System.out.println(name);
//        return null;
//    }

}
